//
//  SportHealthyViewController.m
//  B
//
//  Created by lanou on 16/5/12.
//  Copyright © 2016年 裴启航. All rights reserved.
//

#define kSportURl @"http://app.dunkhome.com/v2/news_v3?c=159&slide=1"

#import "SportHealthyViewController.h"

@interface SportHealthyViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *dataSourceArr;

@end

@implementation SportHealthyViewController

- (NSMutableArray *)dataSourceArr {
    if (!_dataSourceArr) {
        _dataSourceArr = [[NSMutableArray alloc] init];
    }
    return _dataSourceArr;
}
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    tableView.rowHeight = 120;
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [tableView registerNib:[UINib nibWithNibName:@"SportHealthyDetailTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([SportModel class])];
    
    [self.view addSubview:tableView];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:kSportURl parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *sportArr = responseObject[@"table_view_data"];
        for (NSDictionary *dic in sportArr) {
            SportModel *model = [[SportModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataSourceArr addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [tableView reloadData];
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SportModel *model = [self.dataSourceArr objectAtIndex:indexPath.row];
    BaseTableViewCell *cell = [TableViewCellFactory cellWithModelClass:[model class] tableView:tableView index:indexPath];
    [cell setDataWithModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    SportModel *model = [self.dataSourceArr objectAtIndex:indexPath.row];
    SportHealthyDetailViewController *sportHealthy= [SportHealthyDetailViewController new];
    BaseNavigationController *nav = self.view.window.rootViewController.childViewControllers[1];
    sportHealthy.sportDetailID = model.ID.stringValue;
    [nav pushViewController:sportHealthy animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
